package com.etcfg.etlaunch;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.swing.JOptionPane;

public class ApplicationSetting {

	private File pathToSlac;
	private File pathToEtExe;
	private String loadedVersion;
	private boolean firstStart;
	private SlacMode slacMode = SlacMode.AUTOMATIC;
	private boolean checkForUpdatesAtStart = true;
	private File pathToRInput;
	private String mainWindowSettings;

	public boolean isCheckForUpdatesAtStart() {
		return checkForUpdatesAtStart;
	}

	public void setCheckForUpdatesAtStart(boolean checkForUpdatesAtStart) {
		this.checkForUpdatesAtStart = checkForUpdatesAtStart;
	}

	private static String SETTINGS_FILE = System.getProperty("user.home")
			+ "\\etcfgLaunch\\" + "settings.properties";

	public ApplicationSetting() {
		if (new File("settings.properties").exists()) {
			SETTINGS_FILE = "settings.properties";
		} else {
			SETTINGS_FILE = System.getProperty("user.home")
					+ File.separatorChar + "etcfgLaunch" + File.separatorChar
					+ "settings.properties";
		}
	}

	public static final String VERSION = "LOCAL_BUILD_VERSION";

	public SlacMode getSlacMode() {
		return slacMode;
	}

	public void setSlacMode(SlacMode slacMode) {
		this.slacMode = slacMode;
	}

	public File getPathToSlac() {
		return pathToSlac;
	}

	public void setPathToSlac(File pathToSlac) {
		this.pathToSlac = pathToSlac;
	}

	public File getPathToEtExe() {
		return pathToEtExe;
	}

	public void setPathToEtExe(File pathToEtExe) {
		this.pathToEtExe = pathToEtExe;
	}

	public void save() {
		Properties properties = new Properties();
		if (pathToEtExe != null) {
			properties.setProperty("et", pathToEtExe.getAbsolutePath());
		}
		if (pathToSlac != null) {
			properties.setProperty("slac", pathToSlac.getAbsolutePath());
		}
		if (pathToRInput != null) {
			properties.setProperty("rinput", pathToRInput.getAbsolutePath());
		}
		properties.setProperty("version", VERSION);
		properties.setProperty("slacMode", getSlacMode().name());
		properties.setProperty("checkForUpdates", checkForUpdatesAtStart + "");
		properties.setProperty("mainWindowSettings", mainWindowSettings);

		File settings = new File(SETTINGS_FILE);
		if (!settings.exists()) {
			try {
				settings.getParentFile().mkdirs();
				settings.createNewFile();
				settings.setWritable(true);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			properties.store(new FileOutputStream(settings), "Last modified "
					+ new Date());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e);

		}
	}

	public void load() {
		Properties properties = new Properties();

		try {

			FileInputStream fileInputStream = new FileInputStream(SETTINGS_FILE);
			properties.load(fileInputStream);
			String et = properties.getProperty("et");
			String slac = properties.getProperty("slac");
			String rInput = properties.getProperty("rinput");
			loadedVersion = properties.getProperty("version");
			mainWindowSettings = properties.getProperty("mainWindowSettings");
			String slacModeString = properties.getProperty("slacMode");
			String property = properties.getProperty("checkForUpdates", "true");
			System.out.println("Check for updates");
			checkForUpdatesAtStart = Boolean.parseBoolean(property);
			if (slacModeString != null) {
				try {
					slacMode = SlacMode.valueOf(slacModeString);
				} catch (Exception ex) {
					slacMode = SlacMode.AUTOMATIC;
				}
			}
			pathToEtExe = new File(et);
			pathToSlac = new File(slac);
			if (rInput != null) {
				pathToRInput = new File(rInput);
			}
		} catch (Exception e) {
			firstStart = true;

		}
	}

	public String getLoadedVersion() {
		return loadedVersion;
	}

	public void setLoadedVersion(String loadedVersion) {
		this.loadedVersion = loadedVersion;
	}

	public boolean isFirstStart() {
		return firstStart;
	}

	public void setFirstStart(boolean firstStart) {
		this.firstStart = firstStart;
	}

	public File getPathToRInput() {
		return pathToRInput;
	}

	public void setPathToRInput(File pathToRInput) {
		this.pathToRInput = pathToRInput;
	}

	public String getMainWindowSettings() {
		return mainWindowSettings;
	}

	public void setMainWindowSettings(String mainWindowSettings) {
		this.mainWindowSettings = mainWindowSettings;
	}

	public enum SlacMode {
		AUTOMATIC, ALWAYS_OFF, ALWAYS_ON;
	}

}
